package com.example.spark.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.spark.dao.CooperatorDao;
import com.example.spark.entity.Cooperator;
import com.example.spark.entity.Direction;
import com.example.spark.entity.result.CommonResult;
import com.example.spark.service.CooperatorService;
import com.example.spark.service.minio.MinioCloudStorageService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.baomidou.mybatisplus.core.toolkit.IdWorker.getId;

@Service
public class CooperatorServiceImpl extends ServiceImpl<CooperatorDao, Cooperator> implements CooperatorService {

    @Autowired
    private CooperatorDao cooperatorDao;
    @Resource
    MinioCloudStorageService minioCloudStorageService;

    //查询合作单位
    @Override
    public CommonResult selectCooperator(Long id) {
        Cooperator cooperator = cooperatorDao.selectById(id);
        if (cooperator != null) {
            return new CommonResult(200, "查询成功", cooperator);
        }
        return new CommonResult(200, "查询失败");
    }

    //新增合作单位
    @Override
    public CommonResult addCooperator(Cooperator cooperator) {
        String cooperatorName = cooperator.getCooperatorName();

        Map<String, Object> map = new HashMap<>();
        map.put("cooperator_name", cooperatorName);
        List<Cooperator> cooperators = cooperatorDao.selectByMap(map);
        if (cooperators != null && cooperators.isEmpty()) {
            int rowsAffected = cooperatorDao.insert(cooperator);
            if (rowsAffected > 0) {
                return new CommonResult(200, "添加成功");
            } else {
                return new CommonResult<>(200, "添加失败");
            }
        }
        return new CommonResult(200, "添加失败");
    }

    //删除合作单位
    @Override
    public CommonResult deleteCooperator(Long id) {
        Cooperator cooperator = cooperatorDao.selectById(id);
        if (cooperator != null) {
            cooperatorDao.deleteById(id);
            return new CommonResult(200, "删除成功");
        }
        return new CommonResult(200, "删除失败");
    }

    //修改合作单位
    @Override
    public CommonResult updateCooperator(Cooperator cooperator) {
        Long id = cooperator.getId();
        Cooperator cooperator1 = cooperatorDao.selectById(id);
        if (cooperator1 != null) {
            cooperatorDao.updateById(cooperator);
            return new CommonResult(200, "修改成功", cooperator);
        }
        return new CommonResult(200, "修改失败");
    }

    //图片处理
    @Override
    public CommonResult addPhoto(MultipartFile photoFile, Long id) throws IOException {

        String extension = FilenameUtils.getExtension(photoFile.getOriginalFilename());
        String url = minioCloudStorageService.uploadPhotoSuffix(photoFile.getBytes(), extension, String.valueOf(getId()));
        Cooperator cooperator = cooperatorDao.selectById(id);
        cooperator.setPhoto(url);
        int is = cooperatorDao.updateById(cooperator);
        if (is > 0) {
            return new CommonResult(200, "添加图片成功");
        }
        return new CommonResult(200, "添加图片失败");
    }
}
